package com.ruoyi.project.gxgw_modules.gtype.controller;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.gxgw_modules.adsense.domain.Adsense;
import com.ruoyi.project.gxgw_modules.goods.domain.Goods;
import com.ruoyi.project.gxgw_modules.gtype.domain.Gtype;
import com.ruoyi.project.gxgw_modules.gtype.service.IGtypeService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;

/**
 * 商品种类 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-01-20
 */
@Controller
@RequestMapping("/gxgw_modules/gtype")
public class GtypeController extends BaseController {
	private String prefix = "gxgw_modules/gtype";

	@Autowired
	private IGtypeService gtypeService;

	@RequiresPermissions("gxgw_modules:gtype:view")
	@GetMapping()
	public String gtype() {
		return prefix + "/gtype";
	}

	/**
	 * 查询商品种类列表
	 */
	@RequiresPermissions("gxgw_modules:gtype:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Gtype gtype) {
		startPage();
		List<Gtype> list = gtypeService.selectGtypeList(gtype);
		return getDataTable(list);
	}
	
	

	/**
	 * 导出商品种类列表
	 */
	@RequiresPermissions("gxgw_modules:gtype:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(Gtype gtype) {
		List<Gtype> list = gtypeService.selectGtypeList(gtype);
		ExcelUtil<Gtype> util = new ExcelUtil<Gtype>(Gtype.class);
		return util.exportExcel(list, "gtype");
	}

	/**
	 * 新增商品种类
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存商品种类
	 */
	@RequiresPermissions("gxgw_modules:gtype:add")
	@Log(title = "商品种类", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Gtype gtype) {
		Date date = new Date();
		Timestamp timeStamp = new Timestamp(date.getTime());
		gtype.setCreateDate(timeStamp);
		gtype.setCreateBy(ShiroUtils.getSysUser().getUserName());
		return toAjax(gtypeService.insertGtype(gtype));
	}

	/**
	 * 修改商品种类
	 */
	@GetMapping("/edit/{gtypeId}")
	public String edit(@PathVariable("gtypeId") Integer gtypeId, ModelMap mmap) {
		Gtype gtype = gtypeService.selectGtypeById(gtypeId);
		mmap.put("gtype", gtype);
		return prefix + "/edit";
	}

	/**
	 * 修改保存商品种类
	 */
	@RequiresPermissions("gxgw_modules:gtype:edit")
	@Log(title = "商品种类", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Gtype gtype) {
		return toAjax(gtypeService.updateGtype(gtype));
	}

	/**
	 * 删除商品种类
	 */
	@RequiresPermissions("gxgw_modules:gtype:remove")
	@Log(title = "商品种类", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		// 查询每个id下面否有商品，如果有不允许删除
				String[] id = ids.split(",");
				int flag = 0;
				for (int i = 0; i < id.length; i++) {
					Integer gtypeIds = Integer.parseInt(id[i]);
					if (gtypeService.selectCountGoodById(gtypeIds) > 0) {
						flag = 1;
						return error(1, "该种类已被使用,不允许删除！");
					}
					if (flag == 1) {
						break;
					}
				}
		return toAjax(gtypeService.deleteGtypeByIds(ids));
	}
	  @PostMapping("/checkGtypeNameUnique")
	  @ResponseBody
	    public String checkGtypeNameUnique(Gtype gtype)
	    {
		  
	        return  gtypeService.checkGtypeNameUnique(gtype);
	    }

}
